<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="phone">
              <el-input v-model="filters.phone" clearable placeholder="手机号"></el-input>
            </el-form-item>
            <el-form-item prop="idCard">
              <el-input v-model="filters.idCard" clearable placeholder="身份证号"></el-input>
            </el-form-item>
            <el-form-item prop="userType">
              <el-select v-model="filters.userType" clearable placeholder="用户类型">
                <el-option v-for="item in userTypeList" :label="item.name" :value="item.id" :key="item.id"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="openState">
              <el-select v-model="filters.openState" clearable placeholder="开户状态">
                <el-option v-for="item in openStateList" :label="item.name" :value="item.id" :key="item.id"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <!-- 列表 -->
      <el-table :data="dataList" :fit="true" highlight-current-row v-loading="listLoading" element-loading-text="数据加载中，请稍后..." empty-text="对不起，没有找到相关数据！">
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
        <el-table-column label="用户类型" prop="type" align="center">
          <template slot-scope="scope">
            <span>{{ formatUserType(scope.row.userType) }}</span>
            <!-- <el-tag :type="formatBillTypes(scope.row.billType)" effect="dark" size="small">
                            {{ formatBillType(scope.row.billType) }}
                        </el-tag> -->
          </template>
        </el-table-column>
        <el-table-column label="翼码用户ID" prop="merchantId" align="center"> </el-table-column>
        <el-table-column label="手机号" prop="phone" align="center"> </el-table-column>
        <el-table-column label="真实姓名" prop="fullName" align="center"> </el-table-column>
        <el-table-column label="身份证号" prop="idCard" align="center"> </el-table-column>
        <el-table-column label="银行卡类型" prop="cardType" align="center">
          <template slot-scope="scope">
            <div>
              {{ scope.row.cardType == 1 ? '对公' : scope.row.userType == 3 ? '对私个人' : '对私法人' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="银行名称" prop="bankName" align="center"> </el-table-column>
        <el-table-column label="支行名称" prop="branchName" align="center"> </el-table-column>
        <el-table-column label="企业名称/法人姓名/个人姓名" prop="cardName" align="center"> </el-table-column>
        <el-table-column label="开户状态" align="center">
          <template slot-scope="scope">
            <el-tag size="mini" effect="dark" :type="formatOpenStates(scope.row.openState)">
              {{ formatOpenState(scope.row.openState) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createDate" align="center"> </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="mini" v-has="permissionSee" @click="handleClickDetail(scope.row)"> 查看 </el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionRelation" @click.native="handleClickRelation(scope.row)">账号关联</el-dropdown-item>
                <el-dropdown-item v-has="permissionDelete" v-if="scope.row.openState === 3" @click.native="handleClickDelete(scope.row)">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页工具条 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>
<script>
import YmAccountApi from '@/api/account/ymAccount.js';
import * as message from '@/constants/message';
export default {
  name: 'YmAccountManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      listLoading: true,
      filters: {
        userType: '',
        phone: '',
        idCard: '',
        openState: '',
      },
      userTypeList: [
        { id: 1, name: '企业商户' },
        { id: 2, name: '个体工商户' },
        { id: 3, name: '个人' },
      ],
      openStateList: [
        { id: 1, name: '开户审核中' },
        { id: 2, name: '开户成功' },
        { id: 3, name: '开户失败' },
      ],
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },

      permissionSee: 'SYSTEM_MANAGER_ACCOUNT_YMACCOUNT_LIST_SEE',
      permissionRelation: 'SYSTEM_MANAGER_ACCOUNT_YMACCOUNT_LIST_RELATION',
      permissionDelete: 'SYSTEM_MANAGER_ACCOUNT_YMACCOUNT_LIST_DELETE',
    };
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    this.listData();
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    init() {
      let _this = this;
      _this.listLoading = true;
      _this.listData();
      _this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },
    getTableHeight() {
      this.$nextTick(function () {
        this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        window.onresize = () => {
          this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        };
      });
    },
    formatOpenStates(val) {
      switch (val) {
        case 1:
          return '';
        case 2:
          return 'success';
        case 3:
          return 'danger';
      }
    },
    formatOpenState(val) {
      switch (val) {
        case 1:
          return '开户审核中';
        case 2:
          return '开户成功';
        case 3:
          return '开户失败';
      }
    },
    formatUserType(val) {
      switch (val) {
        case 1:
          return '企业商户';
        case 2:
          return '个体工商户';
        case 3:
          return '个人';
      }
    },
    handleSizeChange(size) {
      this.page.pageSize = size;
      this.listData();
    },
    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.listData();
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listData();
    },
    handleClickDetail(row) {
      this.$router.push({
        path: '/account/ymAccountDetail',
        query: { id: row.ymAccountId },
      });
    },
    handleClickRelation(row) {
      this.$router.push({
        path: '/account/ymAccountRelation',
        query: { id: row.ymAccountId },
      });
    },
    handleClickDelete(row) {
      this.$confirm(message.MSG_CONFIRM_DELETE, message.TITLE_PROMPT, {}).then(() => {
        let data = {
          id: row.ymAccountId,
          state: -1,
        };
        BusinessDepartApi.deleteYmAccount(data).then((res) => {
          if (data.state != -1) {
            row.state = data.state;
          } else {
            this.listData();
          }
          this.$Utils.notifySuccess();
        });
      });
    },
    listData() {
      // 获取列表数据
      let cond = {
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize,
        phone: this.filters.phone,
        idCard: this.filters.idCard,
        userType: this.filters.userType,
        openState: this.filters.openState,
      };
      this.listLoading = true;
      YmAccountApi.listYmAccount(cond)
        .then((res) => {
          this.dataList = res.data.list;
          this.page.pageTotal = res.data.total;
          this.listLoading = false;
        })
        .catch((err) => {
          this.listLoading = false;
        });
    },
  },
};
</script>
<style lang="scss">
@import '@/assets/css/views/content.scss';
</style>
<style lang="scss" scoped>
.amount-wrapper {
  display: flex;
  align-items: center;

  .el-input {
    width: 200px;
  }

  .el-input,
  .separate {
    flex-shrink: 0;
  }

  .separate {
    width: 14px;
    height: 2px;
    margin: 0 4px;
    background-color: #dcdfe6;
  }
}
</style>
